﻿/*-----------------------------------------------------------------
 * <copyright file="DataExtractionQuery.cs" company="O-RID">
 *     Copyright (c)2010 O-RID Co.,Ltd.
 * </copyright>
 * <author>Zhang Jun</author>
 * <summary> 
 *      数据抽出(基礎データ)  查询
 *      作成日： 11/09/2010
 * </summary>
 *-----------------------------------------------------------------
 */
namespace Orid.Management.Statistics.OperatorUnitMangement.Command.Other
{
    using System;
    using System.Windows;
    using System.Windows.Media;
    using Orid.Management.Statistics.OperatorUnitMangement.Common.FunctionList;
    using Orid.Management.Statistics.OperatorUnitMangement.Common.SearchCtrl;
    using Orid.Management.Statistics.OperatorUnitMangement.WCFLog4Ref;
    using Orid.Management.Statistics.OperatorUnitMangement.WCFServers.OthersQueryRef;
    using childWindow = Orid.Management.Statistics.OperatorUnitMangement.Common.Window;
    using Wcf = Orid.Management.Statistics.OperatorUnitMangement.Common.Service;

    /// <summary>
    /// 数据抽出(基礎データ) 查询
    /// </summary>
    public class DataExtractionQuery:Orid.Management.Statistics.OperatorUnitMangement.Command.ICommand
    {
        /// <summary>
        /// 数据抽出 查询服务定义 wcfTheDataExtractionQueryClient
        /// </summary>
        private WCFTheOthersQueryClient wcfTheDataExtractionQueryClient;
       
        /// <summary>
        /// 查询控件对象 DataExtractionContainer
        /// </summary>
        private DataExtractionContainer container;

        /// <summary>
        /// 错误提示对话框 errorMessbox
        /// </summary>
        private childWindow.MessageBox errorMessbox;
      
        /// <summary>
        /// 日志服务定义 wcfLogClient
        /// </summary>
        private WCFLog4Client wcfLogClient;

        /// <summary>
        /// 构造函数 DataExtractionQuery
        /// </summary>
        /// <param name="container">查询控件 container</param>
        public DataExtractionQuery(Common.SearchCtrl.DataExtractionContainer container)
        {
            this.container = container;

            this.InitField();
        }

        /// <summary>
        /// 查询完成事件 ExecOver
        /// </summary>
        public event EventHandler<DataExtractionInforCompletedEventArgs> ExecOver;

        /// <summary>
        /// 查询开始事件 ExecBegin
        /// </summary>
        public event EventHandler<EventArgs> ExecBegin;

        /// <summary>
        /// 查询完成事件 OnExecover
        /// </summary>
        /// <param name="sender">Sender of Object</param>
        /// <param name="e">e type of DataExtractionInforCompletedEventArgs</param>
        protected void OnExecover(object sender, DataExtractionInforCompletedEventArgs e)
        {
            if (this.ExecOver != null)
            {
                this.ExecOver(this, e);
            }
        }

        /// <summary>
        /// 查询开始事件 OnExecBegin
        /// </summary>
        /// <param name="sender">Sender of Object</param>
        /// <param name="e">e type of EventArgs</param>
        protected void OnExecBegin(object sender, EventArgs e)
        {
            if (this.ExecBegin != null)
            {
                this.ExecBegin(this, e);
            }
        }

        /// <summary>
        /// 初始化变量 InitField
        /// </summary>  
        private void InitField()
        {
            this.wcfTheDataExtractionQueryClient = Wcf.Service.TheDataExtractionQuery;

            this.errorMessbox = new childWindow.MessageBox();

            this.wcfLogClient = Wcf.Service.Log4;

            // 数据抽邮 查询(委托)
            this.wcfTheDataExtractionQueryClient.DataExtractionInforCompleted += new EventHandler<DataExtractionInforCompletedEventArgs>(this.OnExecover);
        }

        #region ICommand 成员
        /// <summary>
        /// 数据抽出 Exec
        /// </summary>
        public void Exec()
        {

            // this.tbOPID.Text dpDateFrom dpDateTo内容必选的判断
            // 日期不能相同
            // 开始日期不能小于结束日期
            int error = FunctionClass.IsSelectDo(
                                        this.container.dpDateFrom,
                                        this.container.dpDateTo,
                                        this.errorMessbox);

            if (error == 0)
            {
                this.container.btnSearch.IsEnabled = true;
                return;
            }

            // 组合客户端的WHERE条件
            WCFServers.OthersQueryRef.WhereParameters fitWhereData = new WCFServers.OthersQueryRef.WhereParameters();
            // order状态
            bool isOrder;

            try
            {
                try
                {
                    #region  组合条件

                    // 日期的判断条件
                    // 日期返回空值用"null"
                    if (!String.IsNullOrEmpty(this.container.dpDateFrom.Text))
                    {
                        fitWhereData.WhereentryDateFrom = this.container.dpDateFrom.SelectedDate.Value.ToString("yyyy-MM-dd");
                    }
                    else
                    {
                        fitWhereData.WhereentryDateFrom = "null";
                    }

                    if (!String.IsNullOrEmpty(this.container.dpDateTo.Text))
                    {
                        fitWhereData.WhereentryDateTo = this.container.dpDateTo.SelectedDate.Value.ToString("yyyy-MM-dd");
                    }
                    else
                    {
                        fitWhereData.WhereentryDateTo = "null";
                    }

                    // order状态
                    if (this.container.cbOverMark.SelectedIndex == 0)
                    {
                        //オーダ未完 False
                        isOrder = false;
                    }
                    else 
                    {
                        //オーダ完了 True
                        isOrder = true;
                    }

                    #endregion  组合条件
                }
                catch (Exception ex)
                {
                    this.wcfLogClient.ErrorAsync(((App)Application.Current).LogStr + " 数据抽出(基礎データ) 实现类 DataExtractionQuery 转换查询条件出错" +
                                        ex.Message.ToString());

                    Common.FunctionList.FunctionClass.ShowMessageBox(
                       this.errorMessbox,
                       1,
                       "转换查询条件出错",
                       ex.Message.ToString(),
                       Colors.Gray,
                       true,
                       Visibility.Visible,
                       Visibility.Collapsed);
                    return;
                }

                this.OnExecBegin(null, null);

                // 数据抽出 服务端方法
                this.wcfTheDataExtractionQueryClient.DataExtractionInforAsync(fitWhereData, isOrder);

            }
            catch (Exception ex)
            {
                this.wcfLogClient.ErrorAsync(((App)Application.Current).LogStr +
                            " 数据抽出实现(基礎データ) 类 DataExtractionQuery 调用DataExtractionInforAsync服务出错" +
                            ex.Message.ToString());

                Common.FunctionList.FunctionClass.ShowMessageBox(
                   this.errorMessbox,
                   1,
                   "调用DataExtractionInforAsync服务出错",
                   ex.Message.ToString(),
                   Colors.Gray,
                   true,
                   Visibility.Visible,
                   Visibility.Collapsed);
                return;
            }
        }

        #endregion

    }
}
